<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Safe extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$level = 0;

		if ($this->user['mobile']) {
			$level++;
		}

		if ($this->user['email']) {
			$level++;
		}

		if ($this->user['idno'] && $this->user['name']) {
			$level++;
		}

		if ($this->user['password']) {
			$level++;
		}

		if ($this->user['paypwd']) {
			$level++;
		}

		if ($this->user['pic_front'] && $this->user['pic_back'] && $this->user['pic_hand']) {
			$level++;
		}

		$real = $this->d->find(array('table' => 'pic_apply', 'where' => 'user_id=' . $this->user['id'], 'order' => 'add_time desc'));

		if ($real) {
			$d['is_real'] = 1;
			$d['apply_status'] = $real['status'];
		}
		else {
			$d['is_real'] = 0;
		}

		$d['level'] = $level;
		$d['not_set'] = 6 - $level;
		$this->show(array('header', 'user_left', 'safe', 'footer'), $d);
	}

	public function password()
	{
		$this->show('safe_password');
	}

	public function password_do()
	{
		$old_password = $this->input->post('old_password');

		if (!$old_password) {
			ajax_return(1, '旧密码错误');
		}

		$password = $this->input->post('password');
		$repassword = $this->input->post('repassword');

		if (!is_password($password)) {
			ajax_return(1, '密码错误');
		}

		if (!is_password($repassword)) {
			ajax_return(1, '确认密码错误');
		}

		if ($password != $repassword) {
			ajax_return(1, '两次输入的密码不同');
		}

		if ($this->pwd->verify($old_password, $this->user['password']) != $this->user['password']) {
			ajax_return(1, '旧密码错误');
		}

		$rs = $this->db->where('id', $this->user['id'])->update('user', array('password' => $this->pwd->encrypt($password)));

		if ($rs) {
			ajax_return(0, '修改成功');
		}
		else {
			ajax_return(1, '修改失败');
		}
	}

	public function paypwd()
	{
		$this->show('safe_paypwd');
	}

	public function paypwd_do()
	{
		$password = $this->input->post('password');
		$repassword = $this->input->post('repassword');

		if (!is_password($password)) {
			ajax_return(1, '密码错误');
		}

		if (!is_password($repassword)) {
			ajax_return(1, '确认密码错误');
		}

		if ($password != $repassword) {
			ajax_return(1, '两次输入的密码不同');
		}

		if ($this->user['paypwd']) {
			$old_password = $this->input->post('old_password');

			if (!$old_password) {
				ajax_return(1, '旧密码错误');
			}

			if (!$this->pwd->verify($old_password, $this->user['paypwd'])) {
				ajax_return(1, '旧密码错误');
			}
		}

		$rs = $this->db->where('id', $this->user['id'])->update('user', array('paypwd' => $this->pwd->encrypt($password)));

		if ($rs) {
			ajax_return(0, '设置成功');
		}
		else {
			ajax_return(1, '设置失败');
		}
	}

	public function mobile()
	{
		$d['area'] = $this->db->get('area')->result_array();
		$this->show('safe_mobile', $d);
	}

	public function mobile_do()
	{
		$area_code = intval($this->input->post('area_code'));

		if ($area_code <= 0) {
			ajax_return(1, '请选择所在地');
		}

		$mobile = $this->input->post('mobile');

		if (!$mobile) {
			ajax_return(1, '手机号码不能为空');
		}

		$mobile_captcha = $this->input->post('mobile_captcha');

		if (!$mobile_captcha) {
			ajax_return(1, '短信验证码不能为空');
		}

		$s_mobile = $this->session->mobile;

		if (!$s_mobile) {
			ajax_return(1, '验证失败');
		}

		$s_mobile_captcha = $this->session->mobile_captcha;

		if (!$s_mobile_captcha) {
			ajax_return(1, '验证失败');
		}

		if ($this->user['mobile']) {
			$s_old_mobile_captcha = $this->session->old_mobile_captcha;

			if (!$s_old_mobile_captcha) {
				ajax_return(1, '验证失败');
			}

			$old_mobile_captcha = $this->input->post('old_mobile_captcha');

			if (!$old_mobile_captcha) {
				ajax_return(1, '验证失败');
			}

			if ($s_old_mobile_captcha != $old_mobile_captcha) {
				ajax_return(1, '旧手机错误');
			}
		}

		if (($mobile != $s_mobile) || ($mobile_captcha != $s_mobile_captcha)) {
			ajax_return(1, '新手机验证错误');
		}

		$rs = $this->db->where('id', $this->user['id'])->set('mobile', $mobile)->set('area_code', $area_code)->update('user');

		if ($rs) {
			ajax_return(0, '设置成功');
		}
		else {
			ajax_return(1, '设置失败');
		}
	}

	public function email()
	{
		$this->show('safe_email');
	}

	public function email_do()
	{
		$email = $this->input->post('email');

		if (!$email) {
			ajax_return(1, '邮箱地址不能为空');
		}

		$email_captcha = $this->input->post('email_captcha');

		if (!$email_captcha) {
			ajax_return(1, '邮箱验证码不能为空');
		}

		$s_email = $this->session->email;

		if (!$s_email) {
			ajax_return(1, '验证失败');
		}

		$s_email_captcha = $this->session->email_captcha;

		if (!$s_email_captcha) {
			ajax_return(1, '验证失败');
		}

		if ($this->user['email']) {
			$s_old_email_captcha = $this->session->old_email_captcha;

			if (!$s_old_email_captcha) {
				ajax_return(1, '验证失败');
			}

			$old_email_captcha = $this->input->post('old_email_captcha');

			if (!$old_email_captcha) {
				ajax_return(1, '验证失败');
			}

			if ($s_old_email_captcha != $old_email_captcha) {
				ajax_return(1, '旧邮箱错误');
			}
		}

		if (($email != $s_email) || ($email_captcha != $s_email_captcha)) {
			ajax_return(1, '新邮箱验证错误');
		}

		$rs = $this->db->where('id', $this->user['id'])->update('user', array('email' => $email));

		if ($rs) {
			ajax_return(0, '设置成功');
		}
		else {
			ajax_return(1, '设置失败');
		}
	}

	public function real()
	{
		$d['area'] = $this->db->get('area')->result_array();
		$this->show('safe_real', $d);
	}

	public function real_do()
	{
		$area_code_idno = intval($this->input->post('area_code_idno'));

		if ($area_code_idno <= 0) {
			ajax_return(1, '请选择区域');
		}

		$idtype = intval($this->input->post('idtype'));

		if (!in_array($idtype, array(0, 1, 2))) {
			ajax_return(1, '请选择证件类型');
		}

		$idno = $this->input->post('idno');

		if ($idtype == 0) {
			if (!is_idno($idno)) {
				ajax_return(1, '证件号码错误');
			}
		}
		else if (!$idno) {
			ajax_return(1, '证件号码错误');
		}

		$name = $this->input->post('name');

		if (!$name) {
			ajax_return(1, '姓名不能为空');
		}

		if ($this->user['name'] && $this->user['idno']) {
			ajax_return(1, '您已经实名认证了');
		}

		$rs = $this->db->where('id', $this->user['id'])->update('user', array('idno' => $idno, 'name' => $name));

		if ($rs) {
			ajax_return(0, '设置成功');
		}
		else {
			ajax_return(1, '设置失败');
		}
	}

	public function pic()
	{
		$d['area'] = $this->db->get('area')->result_array();
		$d['pic_apply'] = $this->db->where('user_id', $this->user['id'])->order_by('add_time desc')->get('pic_apply')->row_array();
		$this->show(array('header', 'user_left', 'safe_pic', 'footer'), $d);
	}

	public function pic_do()
	{
		$area_code_idno = intval($this->input->post('area_code_idno'));

		if ($area_code_idno <= 0) {
			ajax_return(1, '请选择区域');
		}

		$idtype = intval($this->input->post('idtype'));

		if (!in_array($idtype, array(0, 1, 2))) {
			ajax_return(1, '请选择证件类型');
		}

		$idno = $this->input->post('idno');

		if ($idtype == 0) {
			if (!is_idno($idno)) {
				ajax_return(1, '证件号码错误');
			}
		}
		else if (!$idno) {
			ajax_return(1, '证件号码错误');
		}

		$name = $this->input->post('name');

		if (!$name) {
			ajax_return(1, '姓名不能为空');
		}

		$pic_front = $this->upload_pic('pic_front');
		$pic_back = $this->upload_pic('pic_back');
		$pic_hand = $this->upload_pic('pic_hand');

		if ($pic_front === false) {
			ajax_return(1, '证件正面照上传失败');
		}

		if ($pic_back === false) {
			ajax_return(1, '证件背面照上传失败');
		}

		if ($pic_hand === false) {
			ajax_return(1, '手持证件照上传失败');
		}

		if ($this->user['name'] && $this->user['idno'] && $this->user['pic_front'] && $this->user['pic_back'] && $this->user['pic_hand']) {
			ajax_return(1, '您已经实名认证了');
		}

		$pic_apply = $this->db->where('user_id', $this->user['id'])->order_by('add_time desc')->get('pic_apply')->row_array();
		if ($pic_apply && ($pic_apply['status'] == 0)) {
			ajax_return(1, '实名申请已提交，正在审核' . $this->db->last_query());
		}

		if ($pic_apply && ($pic_apply['status'] == 1)) {
			ajax_return(1, '您已经实名认证了');
		}

		$data['area_code_idno'] = $area_code_idno;
		$data['idtype'] = $idtype;
		$data['idno'] = $idno;
		$data['name'] = $name;
		$data['user_id'] = $this->user['id'];
		$data['username'] = $this->user['username'];
		$data['pic_front'] = $pic_front;
		$data['pic_back'] = $pic_back;
		$data['pic_hand'] = $pic_hand;
		$data['add_time'] = time();
		$rs = $this->db->insert('pic_apply', $data);

		if ($rs) {
			ajax_return(0, '提交成功，请等待审核');
		}
		else {
			ajax_return(1, '提交失败');
		}
	}

	public function upload_pic($file)
	{
		$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/upload/user/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = 5000;
		$config['file_name'] = rand(1000, 9999) . time();
		$config['file_ext_tolower'] = true;
		$this->load->library('upload', $config);

		if (!$this->upload->do_upload($file)) {
			return false;
			$data['error'] = 1;
			$data['msg'] = $this->upload->display_errors();
		}
		else {
			$d = $this->upload->data();
			return $d['file_name'];
			return true;
			$data['error'] = 0;
			$data['msg'] = '上传成功';
		}

		echo json_encode($data);
	}

	public function forget_paypwd()
	{
		$this->show('forget_paypwd');
	}

	public function forget_paypwd_do()
	{
		$email_captcha = $this->input->post('email_captcha');

		if (!$email_captcha) {
			ajax_return(1, '邮箱验证码不能为空');
		}

		$s_email_captcha = $this->session->email_captcha;

		if (!$s_email_captcha) {
			ajax_return(1, '验证失败');
		}

		if ($email_captcha == $s_email_captcha) {
			ajax_return(1, '新邮箱验证错误');
		}

		$password = $this->input->post('password');

		if (!is_password($password)) {
			ajax_return(1, '密码错误');
		}

		$repassword = $this->input->post('repassword');

		if (!is_password($repassword)) {
			ajax_return(1, '确认密码错误');
		}

		if ($password != $repassword) {
			ajax_return(1, '两次输入的密码不同');
		}

		$rs = $this->db->where('id', $this->user['id'])->update('user', array('paypwd' => $this->pwd->encrypt($password)));

		if ($rs) {
			ajax_return(0, '设置成功');
		}
		else {
			ajax_return(1, '设置失败');
		}
	}
}

?>
